import { Controller, Post, Body, Put, Get, UseGuards, Req, Query } from '@nestjs/common';
import { NoteService } from './note.service';
import { Comment } from './types';
import { AuthGuard } from '../guard/auth.guard';
import { CommonResult } from '../utils/result';
import { CommentService } from '../comment/comment.service';
import { UsersService } from '../user/user.service';
import { HttpService } from '@nestjs/axios';
import { lastValueFrom } from 'rxjs';

@Controller('/note')
export class NoteController {
  constructor(
    private readonly noteService: NoteService,
    private readonly commentService: CommentService,
    private readonly usersService: UsersService,
    private readonly httpService: HttpService,
  ) {}

  @Post('/create')
  @UseGuards(AuthGuard)
  async instertNote(@Body() body: Comment, @Req() req) {
    const result = await this.noteService.createComment(body, req.user);
    this.usersService.setCountLevel(req.user.username, 50);
    return result;
  }

  @Get('/getNode')
  @UseGuards(AuthGuard)
  async getNote(@Query('id') id: string, @Req() req) {
    this.usersService.setCountLevel(req.user.username, 30);
    const result = await this.noteService.findNote(id);

    return CommonResult({ ...result }, true, 200, '获取成功');
  }
  @Put('/upldate_like')
  updateNoteLike(@Body() body: any) {
    this.noteService.setLikes(body.noteId);
  }

  @Put('/upldate_see')
  updateNoteSee(@Body() body: any) {
    this.noteService.setSees(body.noteId);
  }

  @Put('/upldate_note')
  @UseGuards(AuthGuard)
  async updateNote(@Body() body: any, @Req() req) {
    if (req.user.id !== '22') {
      return CommonResult(null, false, 403, '无权限弟弟');
    }
    this.noteService.update(body);
    return CommonResult(null, false, 200, '更新成功');
  }

  @Get('/list')
  @UseGuards(AuthGuard)
  getNoteList(@Req() req, @Query('search') search: any) {
    const result = this.noteService.getAllNoteList(req.user.id, search);
    return result;
  }

  @Post('/delete')
  @UseGuards(AuthGuard)
  async deleteNote(@Body() body: any, @Req() req) {
    console.info(body);
    if (req.user.id !== '22') {
      return CommonResult(null, false, 403, '无权限弟弟');
    }
    const result = await this.noteService.deleteNotes(body);
    this.commentService.deleteCommentContent(body.noteId);
    return CommonResult(result, false, 200, '删除成功');
  }

  @Put('/setIsPublic')
  @UseGuards(AuthGuard)
  async setPublic(@Body() body: any, @Req() req) {
    if (req.user.id === '22') {
      await this.noteService.setIsPublic(body);
      return CommonResult(null, true, 200, '设置成功');
    } else {
      return CommonResult(null, false, 403, '无权限弟弟');
    }
  }

  @Get('/weibo')
  async getWeibo() {
    const result = this.httpService.get(
      'https://api.zlinblog.cn/single/rank?platform=微博&rank_name=热搜',
      {
        headers: {
          'X-Licence': '1eOXf4LdJjkGVhc31702183754b08a9eb2f17b9e7aTJyBClCA60049093f3f655',
        },
      },
    );
    return (await lastValueFrom(result)).data;
  }
}
